package com.sdp.wx.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.BusinessContext;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_wz_inventory.entity.WxWzInventory;
import com.sdp.wx.db.wx_wz_inventory.service.IWxWzInventoryService;
import com.sdp.wx.handleImpl.WzInventoryHandlerImpl;
import com.sdp.wx.utils.WxConstants;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-01-31
 */
@RestController
@RequestMapping("/WxWzInventoryController")
public class WxWzInventoryController {

	@Autowired
	private Query query;

	@Autowired
	private IWxWzInventoryService iWxWzInventoryService;

	@Autowired
	private WzInventoryHandlerImpl wzInventoryHandlerImpl;
	
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWxWzInventoryService, WxWzInventory.class, dataCenter));
	}
	
	@PostMapping(value = "/init")
	public Result<?> init(@RequestBody WxWzInventory wzInventory) {
		
		if(wzInventory==null) {
			return Result.error("盘存信息为空");
		}
		BusinessContext businessContext = new BusinessContext();
		businessContext.put(WxWzInventory.class, wzInventory);
		businessContext.put("method", WxConstants.盘存方法_初始化.getCode());
		businessContext = wzInventoryHandlerImpl.handle(businessContext);
		if(businessContext.isSuccess()) {
			return Result.ok(JSONObject.toJSON(businessContext.get(WxWzInventory.class)));
		}else {
			return Result.error(businessContext.getMessage());
		}

	}
	
	
	/**
	 * 完成
	 * @param keyId
	 * @return
	 */
	@GetMapping(value = "/finish")
	public Result<?> finish(@RequestParam(name="keyId",required=true) String keyId) {
		if(keyId==null) {
			return Result.error("请求参数为空");
		}
		BusinessContext businessContext = new BusinessContext();
		businessContext.put("keyId", keyId);
		businessContext.put("method", WxConstants.盘存方法_完成.getCode());
		businessContext = wzInventoryHandlerImpl.handle(businessContext);
		if(businessContext.isSuccess()) {
			return Result.ok(JSONObject.toJSON(businessContext.get(WxWzInventory.class)));
		}else {
			return Result.error(businessContext.getMessage());
		}
	}
	
	/**
	 * 完成调差
	 * @param keyId
	 * @return
	 */
	@GetMapping(value = "/handleDif")
	public Result<?> handleDif(@RequestParam(name="keyId",required=true) String keyId) {
		if(keyId==null) {
			return Result.error("请求参数为空");
		}
		BusinessContext businessContext = new BusinessContext();
		businessContext.put("keyId", keyId);
		businessContext.put("method", WxConstants.盘存方法_完成调差.getCode());
		businessContext = wzInventoryHandlerImpl.handle(businessContext);
		if(businessContext.isSuccess()) {
			return Result.ok(JSONObject.toJSON(businessContext.get(WxWzInventory.class)));
		}else {
			return Result.error(businessContext.getMessage());
		}
	}
}

